let requestUrl;

function addHandler() {
	requestUrl = "/qiling_category/insert";

	$("#qiling_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#qiling_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/qiling_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#equipment_id").combobox("setValue", rowData.equipmentId);

		$("#qiling_dialog").dialog("open");
	} else {
		unselected();
	}
}

function imageHandler() {
	let rowData = $("#qiling_list").datagrid("getSelected");

	if(rowData) {
		$("#category_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function viewHandler() {
	let rowData = $("#qiling_list").datagrid("getSelected");

	if (rowData) {
		$("#property_list").datagrid("reload", {
			equipmentId: rowData.equipmentId
		});

		$("#view_dialog").dialog("open");
	} else {
		unselected();
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#type").combobox({
		url: "/qiling_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#grade").numberspinner({
		min: 60,
		max: 100,
		width: 150,
		increment: 10,
		required: true
	});

	$("#equipment_type").combobox({
		url: "/equipment_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "装备类型",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			if(record) {
				$("#equipment_id").combobox("reload", {
					type: record.id
				});
			}
		}
	});

	$("#equipment_id").combobox({
		url: "/equipment_category/selectList",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='16' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			if(record) {
				$("#grade").numberspinner("setValue", record.grade);
				$("#name").textbox("setValue", record.name + " · 器灵");
				$("#note").textbox("setValue", record.name + "的器灵，可镶嵌融入北斗七星剑，发挥北斗七星剑更强的内在潜能。");
			}
		}
	});

	$("#note").textbox({
		width: 380,
		height: 100,
		required: true,
		multiline: true
	});

	$("#qiling_dialog").dialog({
		title: "器灵类型",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#qiling_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#qiling_dialog").dialog("close");
						$("#qiling_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#qiling_form").form("clear");
				$("#qiling_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#qiling_list", "/qiling_category/upload");

	fetchUploadDialog("/qiling_category/updateImageById", function () {
		$("#qiling_list").datagrid("reload");
	});

	/******************************************************************************************************/

	$("#property_list").datagrid({
		url: "/qiling_view_attribute/selectAttributes",
		method: "get",
		width: 300,
		height: 150,
		fitColumns: true,
		columns: [[
			{field: "propertyName", title: "器灵属性", align: 'center', width: 100,
				formatter: function(value) {
					return "<span class='blue'>" + value +"</span>";
				}
			},
			{field: "range", title: "属性范围", align: 'center', width: 100,
				formatter: function(value, rowData, rowIndex) {
					return "<span>" + rowData.min + "~" + rowData.max +"</span>";
				}
			}
		]]
	});

	$("#view_dialog").dialog({
		title: "器灵详情"
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#qiling_list").datagrid({
		url: "/qiling_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "预览",
			handler: function() {
				viewHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "器灵名称", align: "center", width: 100},
			{field: "type", title: "器灵类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/qiling_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).prop({
							title: data.name,
							src: data.image,
							alt: data.name
						});
					}, error);

					return "<img height='" + size + "' id='type_" + rowIndex + "' />";
				}
			},
			{field: "image", title: "器灵图片", align: "center", width: 100
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "note", title: "器灵说明", align: "center", width: 500},
			{field: "equipmentId", title: "装备类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						ajaxGet("/equipment_type/selectById", {
							id: data.type
						}, function(resp) {
							let result = resp.data;

							$("#equipment_" + rowIndex).prop("title", data.grade + "级" + result.name);
						}, error);

						$("#equipment_" + rowIndex).prop("src", data.image);
					}, error);

					return "<img height='" + size + "' id='equipment_" + rowIndex + "' />";
				}
			}
		]]
	});

});